package graphics.system;
import java.io.Serializable;
import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import tools.Floatmath;
import tools.Point;
public class Node implements Serializable {
	public Point pos;
	public float apos;
	public float boundingrad;
	public Point scale;
	public boolean visible;
	public Node parent;
	public ArrayList<Node> childs;
	public String name;
	public byte zpos;
	public Node(Point pos, Point scale, float apos) {
		this.pos = pos;
		this.apos = apos;
		this.scale = scale;
		visible = true;
		boundingrad = 0;
		zpos = 0;
		childs = new ArrayList<Node>();
		name = "";
	}
	public void setBoundingRad(float rad) {
		this.boundingrad = rad;
	}
	public void setParent(Node n) {
		this.parent = n;
	}
	public Node(Point pos) {
		this(pos, new Point(1, 1), 0);
	}
	public Node(Point pos, float apos) {
		this(pos, new Point(1, 1), apos);
	}
	public void debugDraw() {
		GL11.glPushMatrix();
		setUpGl();
		// if(boundingrad > 0)GLTools.drawCircleLine(Point.ZERO, boundingrad, 0, Color.white, false);
		GLTools.drawLine(new Point(-4, 0), new Point(4, 0), Color.green);
		GLTools.drawLine(new Point(0, -4), new Point(0, 4), Color.green);
		GL11.glPopMatrix();
	}
	public void setUpGl() {
		GL11.glTranslatef(pos.x, pos.y, 0);
		GL11.glRotatef(Floatmath.radToDeg(apos), 0, 0, 1);
		GL11.glScalef(scale.x, scale.y, 1);
	}
	/**
	 * Move to position also moves position for child nodes
	 */
	public void moveAbs(Point newpos) {
		Point dx = newpos.sub(pos);
		pos = newpos;
		for (Node n : childs) {
			n.moveRel(dx);
		}
	}
	/**
	 * Move realative to current position
	 * 
	 * @param newpos
	 */
	public void moveRel(Point delta) {
		pos = pos.add(delta);
		for (Node n : childs) {
			n.moveRel(delta);
		}
	}
	/**
	 * set apos all child nodes will be relatively rotated
	 * 
	 * @param angle
	 */
	public void rotateAbs(float angle) {
		float da = angle - apos;
		apos = angle;
		for (Node n : childs) {
			n.rotatePosRel(pos, da);
		}
	}
	public void rotatePosRel(Point center, float angle) {
		Point dx = pos.sub(center);
		dx.rotate(angle);
		pos = center.add(dx);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
